package com.hupu.jrs.widget

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import com.facebook.drawee.drawable.ProgressBarDrawable

/**
 * Created by sll on 2016/4/1.
 */
class ImageLoadProgressBar @JvmOverloads constructor(private val listener: OnLevelChangeListener? = null, color: Int = Color.GRAY) : ProgressBarDrawable() {

    private var level: Float = 0.toFloat()

    internal var paint = Paint(Paint.ANTI_ALIAS_FLAG)

    internal val oval = RectF()

    internal var radius = 60

    init {
        paint.color = color
    }

    override fun onLevelChange(level: Int): Boolean {
        this.level = level.toFloat()
        listener?.onChange(level)
        invalidateSelf()
        return true
    }

    override fun draw(canvas: Canvas) {
        oval.set((canvas.width / 2 - radius).toFloat(), (canvas.height / 2 - radius).toFloat(),
                (canvas.width / 2 + radius).toFloat(), (canvas.height / 2 + radius).toFloat())
        drawCircle(canvas, level)
    }

    private fun drawCircle(canvas: Canvas, level: Float) {
        val angle = level / 10000 * 360f
        canvas.drawArc(oval, 270f, angle, true, paint)
    }

    interface OnLevelChangeListener {
        fun onChange(level: Int)
    }
}